//
//  GLNavigationController.m
//  Demo
//
//  Created by lishuai on 15/8/13.
//  Copyright (c) 2015年 lishuai. All rights reserved.
//

#import "GLNavigationController.h"

@implementation GLNavigationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp {
    self.navigationBar.barTintColor = [UIColor colorWithRed:230.0f/255.0f green:47.0f/255.0f blue:65.0f/255.0f alpha:1];
    self.navigationBar.titleTextAttributes =  [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
    
    // 返回键
    
    // 根页面，隐藏返回键
    if ([self.viewControllers count] > 1) {
        
        // 获取viewController自己的返回键 CostomView
        UIView *backBtnItemCustomView = nil;
        if ([viewController respondsToSelector:@selector(backBarButtonItemView)]) {
            backBtnItemCustomView = [viewController performSelector:@selector(backBarButtonItemView)];
        }
        
        // 绑定默认事件
        if ([backBtnItemCustomView isKindOfClass:[UIButton class]]) {
            UIButton *backBtn = (UIButton *)backBtnItemCustomView;
            if (backBtn.allTargets.count == 0) {
                [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        
        // 拼装返回键
        UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backBtnItemCustomView];
        viewController.navigationItem.leftBarButtonItem = backBtnItem;
    }
}

- (void)doBack:(id)sender {
    [self popViewControllerAnimated:YES];
}

@end
